//
//  AdvancedBeautySelecView.swift
//  月升
//
//  Created by 拉利法 on 2024/7/10.
//

import UIKit

/// 修改脸型
class AdvancedBeautySelecView: UIView {

    typealias tupleBlock = (_ str: String, _ str1: String)->()
    var selectStrBlock: tupleBlock!
    private var titleArr = [String]()
    private var strArr = [String]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        self.addRoundedCorners(withRadius: 10)
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 0, y: 16, width: self.width, height: 22)
            $0.text = "更改眉形"
            $0.textColor = .white
            $0.textAlignment = .center
            $0.font = FONT(ofSize: 16)
        })
        self.addSubview(titleL)
        
        collectionView.frame = CGRect(x: 10, y: titleL.bottom + 10, width: self.width - 20, height: self.height - 80)
        self.addSubview(collectionView)
        
    }
    
    var typeStr = "" {
        didSet {
            if typeStr.contains("眉") {// 眉毛
                titleArr = ["水平眉", "弧形眉", "上斜眉", "下斜眉"]
                strArr = ["A", "B", "C", "D"]
            } else if typeStr.contains("眼") {// 眼
                titleArr = ["丹凤眼", "桃花眼", "柳叶眼", "杏眼", "细长眼"]
                strArr = ["AAA", "AAB", "ABA", "BBB", "BAA"]
            } else if typeStr == "鼻" {
                titleArr = ["宽鼻", "正常鼻", "窄鼻"]
                strArr = ["A", "B", "C"]
            } else if typeStr == "嘴" {
                titleArr = ["月牙唇", "叶型唇", "微笑唇", "欧美唇"]
                strArr = ["AAA", "ABA", "BAA", "BBA"]
            } else if typeStr == "脸型" {
                titleArr = ["瓜子脸", "方形脸", "椭圆脸", "菱形脸", "圆形脸"]
                strArr = ["AABA", "CAAC", "AABC", "BACC", "BABC"]
            }
        }
    }
    
    
    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 1
        flowLayout.minimumInteritemSpacing = 1
//        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.register(AdvancedBeautySelecViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        return collectionView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension AdvancedBeautySelecView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellW = (self.width - 45) / 3
        return CGSize(width: cellW, height: cellW - 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titleArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AdvancedBeautySelecViewCell
        cell.titleStr = titleArr[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let str = strArr[indexPath.row]
        if selectStrBlock != nil {
            selectStrBlock(str, titleArr[indexPath.row])
            QINPopUpTool.sharedInstance().clear()
        }
    }
    
}



class AdvancedBeautySelecViewCell: UICollectionViewCell {
    
    private var titleL = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = .white
        self.addRoundedCorners(withRadius: 8)
        addUI()
    }
    
    private func addUI() {
        titleL = UILabel().then({
            $0.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height)
            $0.text = "嘻嘻眉"
            $0.textColor = .white
            $0.textAlignment = .center
            $0.font = FONT(ofSize: 14)
        })
        self.addSubview(titleL)
    }
    
    var titleStr = "" {
        didSet {
            titleL.text = titleStr
        }
    }
    var isSelect = false {
        didSet {
            if isSelect {
                self.backgroundColor = ColorWithStr("#FFE8EF")
                titleL.textColor = Color333333
            } else {
                self.backgroundColor = UIColor.clear
                titleL.textColor = .white
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}





/// 选择场景筛选条件
class SelectSceneFilterCriteriaView: UIView {
    
    var selectStrArrBlock: stringArrayBlock!
    var selectStatusStringBlock: stringBlock!
    private var selectStatusArr = [Int]()
    private var selectIndexArr = [Int]()
    private var dataArr = [String]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        self.addRoundedCorners(withRadius: 10)
        dataArr = ["不限妆容", "日常妆容", "知性妆容", "社交妆容", "晚宴/舞会", "表演妆容"]
        selectStatusArr = [0, 0, 0, 0, 0, 0]
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 0, y: 16, width: self.width, height: 22)
            $0.text = "选择场景"
            $0.textColor = .white
            $0.textAlignment = .center
            $0.font = FONT(ofSize: 16)
        })
        self.addSubview(titleL)
        
        collectionView.frame = CGRect(x: 10, y: titleL.bottom + 20, width: self.width - 20, height: 80)
        self.addSubview(collectionView)
        
        let confirmBtn = UIButton().then {
            $0.frame = CGRect(x: 50, y: self.height - 40, width: self.width - 100, height: 30)
            $0.titleLabel?.font = FONT(ofSize: 16)
            $0.setTitle("确定", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addTarget(self, action: #selector(confirmBtnClick), for: .touchUpInside)
        }
        self.addSubview(confirmBtn)
    }
    
    @objc private func confirmBtnClick() {
        guard selectIndexArr.count > 0 else {
            ShowHUD(str: "请进行选择")
            return
        }
        if selectStrArrBlock != nil {
            let newArr = selectIndexArr.map { index in
                dataArr[index]
            }
            selectStrArrBlock(newArr)
            QINPopUpTool.sharedInstance().clear()
        }
        if selectStatusStringBlock != nil {
            selectStatusArr.remove(at: 0)
            let arr = selectStatusArr.map { i in
                "\(i)"
            }
            let str = arr.joined(separator: ",")
            // 如果选择不限妆容,把str转换为11111
            selectStatusStringBlock(str == "0,0,0,0,0" ? "1,1,1,1,1" : str)
        }
    }
    
    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 20
        flowLayout.minimumInteritemSpacing = 10
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.register(AdvancedBeautySelecViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        return collectionView
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension SelectSceneFilterCriteriaView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellW = (self.width - 45) / 3
        return CGSize(width: cellW, height: 30)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AdvancedBeautySelecViewCell
        cell.isSelect = selectIndexArr.contains(indexPath.row)
        cell.titleStr = dataArr[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if selectIndexArr.contains(indexPath.row) {
            selectStatusArr[indexPath.row] = 0
            selectIndexArr.removeAll{$0 == indexPath.row}
        } else {
            // 如果选择不限妆容,其他妆容不做处理
            if selectIndexArr.contains(0) {
                
            } else {
                // 如果选择了其他妆容,再选择不限妆容,不做处理
                if selectIndexArr.count > 0 && indexPath.row == 0 {
                    
                } else {
                    selectIndexArr.append(indexPath.row)
                    selectStatusArr[indexPath.row] = 1
                }
            }
        }
        collectionView.reloadData()
    }
    
}




/// 选择妆容要求筛选条件
class SelectMakeupFilterCriteriaView: UIView {
    
//    typealias stringArrayTupleBlock = (_ arr1: [String], _ arr2: [String])->()
//    var selectStrArrBlock: stringArrayTupleBlock!
    var selectStringBlokc: stringBlock!
    var selectstatusStr1: stringBlock!
    var selectstatusStr2: stringBlock!
    private var selectIndexArr1 = [Int]()
    private var selectIndexArr2 = [Int]()
    private var selectstatusArr1 = [Int]()
    private var selectstatusArr2 = [Int]()
    private var dataArr = [[String]]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        addUI()
    }
    
    private func addUI() {
        self.addRoundedCorners(withRadius: 10)
        dataArr = [
            ["底妆", "遮瑕", "修容", "高光", "定妆", "画眉", "眼影", "睫毛膏", "眼线", "腮红", "口红"],
            ["底妆", "遮瑕", "修容", "高光", "定妆", "画眉", "眼影", "睫毛膏", "眼线", "腮红", "口红"]
        ]
        selectstatusArr1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        selectstatusArr2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        
//        let titleL = UILabel().then({
//            $0.frame = CGRect(x: 0, y: 16, width: self.width, height: 22)
//            $0.text = "选择场景"
//            $0.textColor = .white
//            $0.textAlignment = .center
//            $0.font = FONT(ofSize: 16)
//        })
//        self.addSubview(titleL)
        
        collectionView.frame = CGRect(x: 10, y: 0, width: self.width - 20, height: self.height - 50)
        self.addSubview(collectionView)
        
        let confirmBtn = UIButton().then {
            $0.frame = CGRect(x: 50, y: self.height - 40, width: self.width - 100, height: 30)
            $0.titleLabel?.font = FONT(ofSize: 16)
            $0.setTitle("确定", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addTarget(self, action: #selector(confirmBtnClick), for: .touchUpInside)
        }
        self.addSubview(confirmBtn)
    }
    
    @objc private func confirmBtnClick() {
        if selectStringBlokc != nil {
            let newArr1 = selectIndexArr1.map { index in
                dataArr[0][index]
            }
            let newArr2 = selectIndexArr2.map { index in
                dataArr[1][index]
            }
//            selectStrArrBlock(newArr1, newArr2)
            
            var str1 = ""
            if newArr1.count > 2 {
                str1 = newArr1[0] + "/" + newArr1[1] + "..."
            } else {
                str1 = newArr1.joined(separator: "/")
            }
            
            var str2 = ""
            if newArr2.count > 2 {
                str2 = newArr2[0] + "/" + newArr2[1] + "..."
            } else {
                str2 = newArr2.joined(separator: "/")
            }
            let str = str1 + "\n" + str2
            
            selectStringBlokc(str)
            
            QINPopUpTool.sharedInstance().clear()
        }
        if selectstatusStr1 != nil {
            let arr = selectstatusArr1.map { i in
                "\(i)"
            }
            let str = arr.joined(separator: ",")
            selectstatusStr1(str)
        }
        if selectstatusStr2 != nil {
            let arr = selectstatusArr2.map { i in
                "\(i)"
            }
            let str = arr.joined(separator: ",")
            selectstatusStr2(str)
        }
    }
    
    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.register(AdvancedBeautySelecViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        collectionView.register(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView")
        return collectionView
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension SelectMakeupFilterCriteriaView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        dataArr.count
    }
    
    /// header大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.width, height: 45)
    }
    
    /// 设置header或者foot
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let backView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath)
        if kind == UICollectionView.elementKindSectionHeader {
            backView.removeAllSubviews()
            let titleL = UILabel().then({
                $0.frame = CGRect(x: 0, y: 10, width: self.width, height: 30)
                $0.text = indexPath.section == 0 ? "妆容需包含" : "妆容需强调"
                $0.textColor = .white
                $0.textAlignment = .center
                $0.font = FONT(ofSize: 16)
            })
            backView.addSubview(titleL)
        } else {
            
        }
        return backView
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellW = (collectionView.width - 45) / 4
        return CGSize(width: cellW, height: 20)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AdvancedBeautySelecViewCell
        if indexPath.section == 0 {
            cell.isSelect = selectIndexArr1.contains(indexPath.row)
        } else {
            cell.isSelect = selectIndexArr2.contains(indexPath.row)
        }
        
        cell.titleStr = dataArr[indexPath.section][indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            if selectIndexArr1.contains(indexPath.row) {
                selectIndexArr1.removeAll{$0 == indexPath.row}
                selectstatusArr1[indexPath.row] = 0
            } else {
                selectIndexArr1.append(indexPath.row)
                selectstatusArr1[indexPath.row] = 1
            }
        } else {
            if selectIndexArr2.contains(indexPath.row) {
                selectIndexArr2.removeAll{$0 == indexPath.row}
                selectstatusArr2[indexPath.row] = 0
            } else {
                selectIndexArr2.append(indexPath.row)
                selectstatusArr2[indexPath.row] = 1
            }
        }
       
        collectionView.reloadData()
    }
    
}

